


import 'dart:io';

import 'package:get/get.dart';

import '../models/ai/index.dart';
import '../models/enums/o2_server_module.dart';
import '../models/id_data.dart';
import '../models/o2_api_response.dart';
import '../models/value_data_bool.dart';
import '../utils/log_util.dart';
import '../utils/o2_api_manager.dart';
import '../utils/o2_http_client.dart';

class AIAssembleControlService extends GetxService {
  static AIAssembleControlService get to => Get.find();

  String baseUrl() {
    return O2ApiManager.instance
            .getModuleBaseUrl(O2DistributeModuleEnum.x_ai_assemble_control) ??
        '';
  }

  void closeSSE() {
    try {
        O2HttpClient.instance.endSSE();
    } catch (err, stackTrace) {
      OLogger.e('关闭 AI 消息的 SSE 失败', err, stackTrace);
    }
  }

  Future<void> clueChatSSE(AIChatPost post, O2HttpSSECallback callback) async {
    try {
      await O2HttpClient.instance.postSSE('${baseUrl()}jaxrs/chat/completion', post.toJson(), callback);
    } catch (err, stackTrace) {
      OLogger.e('发送 AI 消息失败', err, stackTrace);
    }
  }

  AIBaseConfig? _baseConfig;
  ///  配置文件读取 
  Future<AIBaseConfig?> config() async {
    try {
      if (_baseConfig != null) {
        return _baseConfig;
      }
      ApiResponse response =
          await O2HttpClient.instance.get('${baseUrl()}jaxrs/config/base/config');
      _baseConfig = AIBaseConfig.fromJson(response.data);
      return _baseConfig;
    } catch (err, stackTrace) {
      OLogger.e('配置文件读取失败', err, stackTrace);
    }
    return null;
  }

  /// 
  Future<List<AIClueInfo>?> clueListByPage(int page, { int size = 100}) async {
    try {
      ApiResponse response = await O2HttpClient.instance.get('${baseUrl()}jaxrs/chat/list/paging/$page/size/$size');
      var list = response.data == null ? [] : response.data as List;
      return list.map((group) => AIClueInfo.fromJson(group)).toList();
    } catch (err, stackTrace) {
      OLogger.e('查询ai会话列表失败', err, stackTrace);
    }
    return null;
  }
  // 更新消息的 extra 内容
  Future<ValueBoolData?> writeChatMsgExtra(Map<String, dynamic> body) async {
    try {
      ApiResponse response = await O2HttpClient.instance.post('${baseUrl()}jaxrs/chat/write/completion/extra', body);
      return ValueBoolData.fromJson(response.data);
    } catch (err, stackTrace) {
      OLogger.e('查询ai附件列表失败', err, stackTrace);
    }
    return null;
  }

  Future<List<AIChatInfo>?> clueChatListByPage(int page, String clueId, {int size = 100}) async {
    try {
      ApiResponse response = await O2HttpClient.instance.get('${baseUrl()}jaxrs/chat/list/completion/$clueId/paging/$page/size/$size');
      var list = response.data == null ? [] : response.data as List;
      return list.map((group) => AIChatInfo.fromJson(group)).toList();
    } catch (err, stackTrace) {
      OLogger.e('查询ai 消息列表失败', err, stackTrace);
    }
    return null;
  }

  /// 附件列表
  Future<List<AIChatAttachmentInfo>?> attachmentList(List<String> ids) async {
    try {
      ApiResponse response = await O2HttpClient.instance.post('${baseUrl()}jaxrs/file/list', {'idList': ids});
      var list = response.data == null ? [] : response.data as List;
      return list.map((group) => AIChatAttachmentInfo.fromJson(group)).toList();
    } catch (err, stackTrace) {
      OLogger.e('查询ai附件列表失败', err, stackTrace);
    }
    return null;
  }
  
  /// 附件下载地址
  String getAIAttachmentURL( String id) {
    return "${baseUrl()}jaxrs/file/$id/download";
  }

  /// 上传附件
  Future<IdData?> uploadAttachment( File file) async {
    try {
      ApiResponse response = await O2HttpClient.instance.postUploadFile('${baseUrl()}jaxrs/file/upload', file, body: {});
      return IdData.fromJson(response.data);
    } catch (err, stackTrace) {
      OLogger.e('上传附件失败', err, stackTrace);
    }
    return null;
  }
}